package com.openbravo.beans;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Dialog;
import java.awt.GraphicsEnvironment;
import java.awt.Font;
import java.awt.Frame;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.border.BevelBorder;
import java.beans.VetoableChangeListener;
import java.beans.PropertyChangeEvent;
import javax.swing.border.TitledBorder;

public class JFontChooser extends JDialog {
	public static int OK_OPTION = 0;
	public static int CANCEL_OPTION = 1;

	private JList fontList, sizeList;
	private JTextArea txtSample;

	private int OPTION;

	private String[] sizes = new String[] { "2", "4", "6", "8", "10", "12",
			"14", "16", "18", "20", "22", "24", "30", "36", "48", "72" };
	private String[] styles = new String[] {"Plain", "Bold", "Italic", "Bold Italic"};
	private JTextField textField;
	private JTextField familySelected;
	private JTextField sizeSelected;
	private JList styleList;
	private JTextField styleSelected;
	private Font selected;

	public int showDialog(Font font) {
		setFont(font);
		return showDialog();
	}

	public int showDialog() {
		setVisible(true);
		return OPTION;
	}

	/**
	 * @wbp.parser.constructor
	 */
	public JFontChooser(Frame parent) {
		super(parent, true);
		initComponents();
	}
	public JFontChooser(Dialog parent) {
		super(parent, true);
		initComponents();
	}

	private void initComponents() {
		setPreferredSize(new Dimension(400, 450));
		setTitle("JFontChooser");
		
				OPTION = JFontChooser.CANCEL_OPTION;
		
				// create all components
		
				JButton btnOK = new JButton("OK");
		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFontChooser.this.OPTION = JFontChooser.OK_OPTION;
				JFontChooser.this.setVisible(false);
			}
		});
		
				JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFontChooser.this.OPTION = JFontChooser.CANCEL_OPTION;
				JFontChooser.this.setVisible(false);
			}
		});
				// add the listeners
				ListSelectionListener listListener = new ListSelectionListener() {
					public void valueChanged(ListSelectionEvent e) {
						txtSample.setFont(getCurrentFont());
					}
				};
		
				// build the container
		
				getContentPane().setLayout(new java.awt.BorderLayout());
		
				JPanel leftPanel = new JPanel();
		leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.X_AXIS));
		
				getContentPane().add(leftPanel, java.awt.BorderLayout.CENTER);
		
				JPanel panel = new JPanel();
		leftPanel.add(panel);
		
				fontList = new JList(GraphicsEnvironment.getLocalGraphicsEnvironment()
						.getAvailableFontFamilyNames());
				fontList.addListSelectionListener(new ListSelectionListener() {
					public void valueChanged(ListSelectionEvent e) {
						txtSample.setFont(getCurrentFont());
						familySelected.setText((String) fontList.getSelectedValue());
						txtSample.setText((String) fontList.getSelectedValue());
					}
				});
				fontList.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
				panel.setLayout(new BorderLayout(0, 0));
				
				JPanel panel_4 = new JPanel();
				panel.add(panel_4, BorderLayout.NORTH);
				panel_4.setLayout(new BoxLayout(panel_4, BoxLayout.Y_AXIS));
						
						JLabel lblNewLabel = new JLabel("Family");
						panel_4.add(lblNewLabel);
				
						familySelected = new JTextField();
						familySelected.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
						familySelected.setEnabled(false);
						familySelected.setEditable(false);
						panel_4.add(familySelected);
						familySelected.setColumns(10);
		
				JScrollPane scrollPane = new JScrollPane(fontList);
		panel.add(scrollPane);
				
						JPanel panel_2 = new JPanel();
						leftPanel.add(panel_2);
								panel_2.setLayout(new BorderLayout(0, 0));
								
								JPanel panel_6 = new JPanel();
								panel_2.add(panel_6, BorderLayout.NORTH);
								panel_6.setLayout(new BoxLayout(panel_6, BoxLayout.Y_AXIS));
								
								JLabel lblNewLabel_2 = new JLabel("Style");
								panel_6.add(lblNewLabel_2);
								
										styleSelected = new JTextField();
										styleSelected.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
										styleSelected.setEnabled(false);
										styleSelected.setEditable(false);
										panel_6.add(styleSelected);
										styleSelected.setColumns(10);
										
												styleList = new JList(styles) ;
												styleList.addListSelectionListener(new ListSelectionListener() {
													public void valueChanged(ListSelectionEvent e) {
														txtSample.setFont(getCurrentFont());
														styleSelected.setText((String) styleList.getSelectedValue());
													}
												});
												styleList.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
												panel_2.add(styleList, BorderLayout.CENTER);
												styleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				JPanel panel_1 = new JPanel();
		leftPanel.add(panel_1);
		
				sizeList = new JList(sizes);
				sizeList.addListSelectionListener(new ListSelectionListener() {
					public void valueChanged(ListSelectionEvent e) {
						sizeSelected.setText((String) sizeList.getSelectedValue());
						txtSample.setFont(getCurrentFont());
					}
				});
				
				sizeList.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				panel_1.setLayout(new BorderLayout(0, 0));
				
				JPanel panel_5 = new JPanel();
				panel_1.add(panel_5, BorderLayout.NORTH);
				panel_5.setLayout(new BoxLayout(panel_5, BoxLayout.Y_AXIS));
				
				JLabel lblNewLabel_1 = new JLabel("Size");
				panel_5.add(lblNewLabel_1);
		
				sizeSelected = new JTextField();
				sizeSelected.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
				sizeSelected.setEnabled(false);
				sizeSelected.setEditable(false);
				panel_5.add(sizeSelected);
		sizeSelected.setColumns(10);
		JScrollPane scrollPane_1 = new JScrollPane(sizeList);
		panel_1.add(scrollPane_1);
		
				JPanel rightPanel = new JPanel();
		rightPanel.setLayout(new java.awt.BorderLayout());
		
				JPanel rightPanelSub2 = new JPanel();
		rightPanelSub2.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
		
				rightPanelSub2.add(btnOK);
		rightPanelSub2.add(btnCancel);
		
				rightPanel.add(rightPanelSub2, java.awt.BorderLayout.SOUTH);
		
				getContentPane().add(rightPanel, BorderLayout.SOUTH);
		
				setSize(400, 416);
		setResizable(false);
		

		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new TitledBorder(null, "Selected Font", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_3.setPreferredSize(new Dimension(10, 80));
		getContentPane().add(panel_3, BorderLayout.NORTH);
		
				txtSample = new JTextArea() {
					public Dimension getPreferredScrollableViewportSize() {
						return new Dimension(385, 80);
					}
				};
				txtSample.setEnabled(false);
				txtSample.setEditable(false);
				panel_3.add(txtSample);
				txtSample.setText("Just a Test");
				setFont(null);
				pack();
	}

	public void setFont(Font font) {
		if (font == null)
			font = txtSample.getFont();
		selected=font;
		fontList.setSelectedValue(font.getName(), true);
		fontList.ensureIndexIsVisible(fontList.getSelectedIndex());
		sizeList.setSelectedValue("" + font.getSize(), true);
		sizeList.ensureIndexIsVisible(sizeList.getSelectedIndex());
		styleList.setSelectedIndex(font.getStyle());
	}

	public Font getFont() {
		if (OPTION == OK_OPTION) {
			return getCurrentFont();
		} else
			return null;
	}

	private Font getCurrentFont() {
		String fontFamily = (String) fontList.getSelectedValue();
		int fontSize = selected.getSize();
		if(sizeList.getSelectedValue()!=null)
			fontSize =Integer.parseInt((String) sizeList.getSelectedValue());
		int fontType =  styleList.getSelectedIndex();
		return new Font(fontFamily, fontType, fontSize);
	}
}
